<template>
  <j-modal title="1688下单" :width="900" :visible.sync="modalVisible" :maskClosable="false">
    <a-divider orientation="left"> 账号信息 </a-divider>
    <a-form-model ref="form" :model="formData" :rules="rules">
      <a-form-model-item label="下单账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseAccount">
        <j-url-select-search
          placeholder="请搜索选择1688下单账号"
          v-model="formData.purchaseAccount"
          :allowClear="false"
          url="/hzkj-purchase/purchase/auth/account/list?pageNo=1&pageSize=10&authStatus=success&enableStatus=Y"
          :optionDiy="optionDiy"
        />
      </a-form-model-item>
      <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiveAddress1">
        <template slot="label">
          收货地址
          <a-tooltip placement="top" :overlayStyle="{ 'min-Width': '280px' }">
            <template slot="title">
              <div>{成本中心}：表示此处在下单时候为采购单的成本中心字段内容(如：XXXXXXX公司)。</div>
              <div>{采购单号}：表示此处在下单时候为采购单的具体单号(如：PO250525000001)。</div>
            </template>
            <a-icon type="info-circle" style="vertical-align: middle" />
          </a-tooltip>
        </template>
        <a-input v-model="formData.receiveAddress1" placeholder="请输入收货地址" :maxLength="255"></a-input>
      </a-form-model-item>
      <a-form-model-item label="追加地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiveAddress2">
        <a-input v-model="formData.receiveAddress2" placeholder="请输入追加地址" :maxLength="255"></a-input>
      </a-form-model-item>
      <a-row>
        <a-col :span="12">
          <a-form-model-item
            label="订单类型"
            :labelCol="{
              xs: { span: 24 },
              sm: { span: 8 },
            }"
            :wrapperCol="wrapperCol"
            prop="orderType"
          >
            <j-search-select-tag-new placeholder="请选择" v-model="formData.orderType" dictCode="purchase_order_type" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item
            label="交易方式"
            :labelCol="{
              xs: { span: 24 },
              sm: { span: 6 },
            }"
            :wrapperCol="wrapperCol"
            prop="transactionWay"
          >
            <j-search-select-tag-new
              placeholder="请选择"
              v-model="formData.transactionWay"
              dictCode="purchase_transaction_way"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <template #footer>
      <a-button @click="modalVisible = false"> 取消 </a-button>
      <a-button type="primary" :loading="submitLoading" @click="handleOk"> 1688下单 </a-button>
    </template>
  </j-modal>
</template>

<script>
import { getAction, postAction, putAction } from '@/api/manage'

export default {
  data() {
    return {
      modalVisible: false,
      submitLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      formData: {},
      addParams: null,
      orderIds: null,
      rules: {
        purchaseAccount: [{ required: true, message: '请选择下单账号!' }],
        receiveAddress1: [{ required: true, message: '请输入收货地址!' }],
        orderType: [{ required: true, message: '请选择订单类型!' }],
        transactionWay: [{ required: true, message: '请选择交易方式!' }],
      },
      url: {
        add: `/hzkj-purchase/purchase/order/info/add`,
        edit: `/hzkj-purchase/purchase/order/info/edit`,
        batchPlaceOrder: '/hzkj-purchase/purchase/order/info/batchPlaceOrder',
        queryWarehouse: '/hzkj-wms/wms/warehouse/queryById',
      },
    }
  },
  methods: {
    api_open(warehouseId, orderIds, addParams) {
      this.modalVisible = true
      this.addParams = addParams // 有这个参数表示走添加接口
      this.orderIds = orderIds
      if (warehouseId) {
        getAction(this.url.queryWarehouse, { id: warehouseId }).then((res) => {
          if (res.success) {
            this.formData = {
              receiveAddress1: `${res.result.address}_{成本中心}_{采购单号}_${res.result.contacts}_${res.result.phone}`,
              receiveAddress2: '',
            }
          } else {
            this.$message.error(res.message)
          }
        })
      }
    },
    optionDiy(result) {
      return result.records.map((item) => {
        return {
          ...item,
          value: item.id,
          text: `${item.name}_${item.enableStatus_dictText}_${item.authStatus_dictText}`,
        }
      })
    },
    // 产品提交
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.submitLoading = true
          let sendData = JSON.parse(JSON.stringify(this.formData))
          console.log(this.formData)
          let url = this.url.batchPlaceOrder
          let request = postAction
          if (this.addParams) {
            sendData = { ...this.addParams, ...sendData }
            if (this.addParams.id) {
              request = putAction
              url = this.url.edit
            } else {
              url = this.url.add
            }
          }
          if (this.orderIds) {
            sendData = { ...sendData, orderIds: this.orderIds }
          }
          request(url, sendData)
            .then((res) => {
              if (res.success) {
                this.$message.success('下单成功')
                this.$emit('submit')
                this.modalVisible = false
              } else {
                this.$message.error(res.message)
              }
            })
            .finally(() => {
              this.submitLoading = false
            })
        }
      })
    },
  },
}
</script>
